package com.vision.web.controller;

import com.alibaba.fastjson.JSON;
import com.vision.cache.impl.OnlineCache;
import com.vision.common.bean.CommonResult;
import com.vision.common.bean.UserMsg;
import com.vision.common.bean.install.InstallOrder;
import com.vision.common.bean.requestlog.RequestLogOrder;
import com.vision.common.util.HttpIPUtil;
import com.vision.service.CommonService;
import com.vision.web.annotation.Decode;
import com.vision.web.annotation.Encode;
import com.vision.web.listener.SessionListenerHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author zhongchao
 * @title
 * @Date 2017/05/22
 */
@RestController
public class HomeController {

    public static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);

    @Resource
    private CommonService commonService;

    @Resource
    private OnlineCache onlineCache;

    @RequestMapping("/msg")
    public String index() {
        List<UserMsg> userSets = onlineCache.getCache();
        return JSON.toJSONString(userSets);
    }

    /**
     * InstallOrder
     *
     * @return
     */
    @Encode
    @RequestMapping("/install")
    public String install(@Decode String params, HttpServletRequest request) {
        String ipAddr = HttpIPUtil.getIpAddr(request);
        LOGGER.info("request ip:{},HomeController.install request params:{}", ipAddr, params);
        InstallOrder installOrder = JSON.parseObject(params, InstallOrder.class);
        installOrder.setDate(new Date());
        CommonResult result = commonService.install(installOrder);
        return JSON.toJSONString(result);

    }

    /**
     * InstallOrder
     *
     * @return
     */
    @Encode
    @RequestMapping("/log")
    public String requestLog(@Decode String params, HttpServletRequest request) {
        String ipAddr = HttpIPUtil.getIpAddr(request);
        LOGGER.info("request ip:{},HomeController.log request params:{}", ipAddr, params);
        RequestLogOrder installOrder = JSON.parseObject(params, RequestLogOrder.class);
        installOrder.setRequestDate(new Date());
        installOrder.setIp(ipAddr);
        CommonResult result = commonService.requestLog(installOrder);
        return JSON.toJSONString(result);
    }

    public static void main(String[] args) {
        // RequestLogOrder requestLogOrder = new RequestLogOrder();
        // requestLogOrder.setRequestDate(new Date());
        // requestLogOrder.setInstallUnique("iusdu");
        // requestLogOrder.setPhone("1827878788");

        InstallOrder installOrder = new InstallOrder();
        installOrder.setActivateCode("111");
        installOrder.setDate(new Date());
        installOrder.setImei("ejh98w2398");
        installOrder.setUnique("slkd");
        System.out.println(JSON.toJSONString(installOrder));
    }
}
